<?php
class BookCourseServer {
	public function pagingBySearch($where="",$order="",$numberEachPage = 10){
		
		$count = HttpDaoManager::getBookCourseDao()->searchMutilNumber($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		if(empty($order)){
			$order = 'order by a.id desc ';
		}
		$list =HttpDaoManager::getBookCourseDao()->searchMutilList($where,$order, $limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}
	/**
	 * id:老师开课id ,userClassType:学生课时类型,type:课程类型,chapter:章节,userId:会员id,message:学生留言,communicationTools:通讯工具
	 */
	public function booking($booking_info){
		//判断是否过期
		$id = $booking_info['teacher_class_id'];
		$userClassType = $booking_info['user_class_type'];
		$type = $booking_info['course_type'];
		$chapter_no = $booking_info['chapter_no'];
		$chapter_name = $booking_info['chapter_name'];
		$chapter = $booking_info['chapter_id'];
		$userId = $booking_info['user_id'];
		$message = $booking_info['message'];
		$communicationTools = $booking_info['communicationTools'];

		$bookdate  = HttpServerManager::getTeacherStartCourseServer()->userGet($id);
		if ($bookdate==TEACHER_ERROR_NOE){
			return  TEACHER_ERROR_NOE;
		}
		
		//排他
// 		$exclusive =$this->exclusive($bookdate['date'], $bookdate['time'], $userId);
// 		if (!($exclusive===true)){
// 			return  $exclusive;
// 		}

		if($userClassType=='user_monthly_class'){
			// 判断包月预约
			$WNMonthlyBooking = $this->WNMonthlyBooking($bookdate['date'], $userId);
			if (!($WNMonthlyBooking===true)){
				return  $WNMonthlyBooking;
			}
		}

		//通过 type,chapter 获取课程判断课程是否符合规则
		$coursedata= HttpServerManager::getCourseServer()->subBookGetCourse($type, $chapter);
		if (!is_array($coursedata)){
			return $coursedata; 
		}
		
		//判断钱够不够 扣除金钱
		$userData = HttpDaoManager::getUserDao()->get($userId);
		if ($userData[$userClassType]<1){//单元设置为1吧
			return USER_ERROR_FIVE;
		}
// 		$qq = $userData['qq'];			
// 		$skype_id = $userData['skype_id'];			
// 		$phone = $userData['phone'];
					
		$qq = $booking_info['qq'];
		$skype_id = $booking_info['skype_id'];
		$phone = $booking_info['phone'];
		
		$english_name = $booking_info['english_name'];
		$classHour = $userData[$userClassType] -1;
		$userData[$userClassType] = $classHour;

		$booktype = Common::_t('book_type');
		$data = array(
				'book_type'           => $booktype[$userClassType],
				'pay_userclass'       => 1,
				'course_needclass'    => 1,
				'user_id'             => $_SESSION['user']['id'],
				'teacher_id'          => $bookdate['teacher_id'],
				'teacher_class_id'    => $id,
				'course_type'         => $type,
				'course_id'           => $coursedata['id'],
				'chapter_id'           => $chapter,
				'chapter_no'           => $chapter_no,
				'chapter_name'           => $chapter_name,
				'message'             => $message,
				'book_date'           => $bookdate['date'],
				'book_time'           => $bookdate['time'],
				'communication_tools' => $communicationTools,
				'skype_id'=>$skype_id,
				'qq'=>$qq,
				'phone'=>$phone,
				'english_name'=>$english_name
		);
		$bookId  = $this->add($data);
		HttpDaoManager::getUserDao()->update($userData, $_SESSION['user']['id']);//更新用户账户金钱信息
		HttpDaoManager::getTeacherStartCourseDao()->updateStatus(TEACHER_CLASS_STATUS_BOOKING, $id);//更新老师开课单

		$add_user='buyer';//默认用户添加
		
		$tmp = array(
				'type'       => USER_CLASS_HOUR_LOG_1,
				'class_type' => $booktype[$userClassType],
				'target_id'  => $bookId,
				'class_hour' => -1,
				'user_id'    => $_SESSION['user']['id'],
				'add_user'   => $add_user,
				'remark'     => '预定课程'
		);
		HttpServerManager::getUserClassHourLogServer()->add($tmp);
		$_SESSION['book_course_id'] = $bookId;
		return true;
	}
	public function add($data){
		$data['book_no']  = $this->getBookNo();	
		$data['add_time'] = time();
		$data['add_user'] = 'buyer';//前台默认\后台需更改
		return  HttpDaoManager::getBookCourseDao()->add($data);
	}
	public function getBookNo(){
		$bookNo = 'BC'.ClassManager::getToos()->new_sn();
		$tmp =  HttpDaoManager::getBookCourseDao()->getByBookNo($bookNo);
		if (!empty($tmp)){
			$bookNo =  $this->getBookNo();
		}
		return  $bookNo;
	}
	public function exclusive ($date , $time , $userId){
		$tmp = array(
				'book_date' => $date,
				'book_time' => $time,
				'user_id'   => $userId,
				'class_status' => array('3','!=')
		);
		$userBook = HttpDaoManager::getBookCourseDao()->searchList($tmp);
		if (empty($userBook)){
			return  true;
		}else{
			return  USER_ERROR_SIX;
		}
	}
	public function WNMonthlyBooking($date,$userId){
		$status_str_list = CLASS_STATUS_0.",".CLASS_STATUS_1;
		$tmp = array(
			'class_status' => array($status_str_list,'in'),
			'book_type'    => USER_MONTHLY_CLASS,
			'user_id'	=> $userId
		);
		
		$order_by = ' order by id desc ';
		$todayUserBook = HttpDaoManager::getBookCourseDao()->searchList($tmp,$order_by);//检查未完成的课程
		if (!empty($todayUserBook)) {
			$today_date= strtotime(date('Y-m-d'));
			$now_time = date('Hi');
			$book_time = $todayUserBook[0]['book_time']+25;
			
			if($todayUserBook[0]['class_status']!=CLASS_STATUS_2){//若未完成
				if($today_date<$todayUserBook[0]['book_date']){//若当前时间小于预约单时间
					return  USER_ERROR_22;//不能再预约
				}elseif($today_date==$todayUserBook[0]['book_date']){
					if($now_time<=$book_time){
						return  USER_ERROR_22;//不能再预约
					}				
				}			
			}
		}
		
		$tmp = array(
				'book_date'=>$date,
				'class_status' => array(CLASS_STATUS_3,'!='),
				'book_type'    => USER_MONTHLY_CLASS,
				'user_id'	=> $userId
		);

		$todayCompleteUserBook = HttpDaoManager::getBookCourseDao()->searchList($tmp);//检查当日是否课为唯一
		if(!empty($todayCompleteUserBook)){
			return  USER_ERROR_SEVEN;//当天已经上过课了
		}
		
		return true;
	}
	
	public function saveRemark($id,$remark =''){
		$tmp = array(
			'type' =>BOOK_COURSE_LOG_TYPE_5,
			'remark' =>$remark,
			'book_course_id' => $id
		);
		HttpServerManager::getBookCourseLogServer()->add($tmp);
	}
	
	public function confirm($id,$remark =''){
		$data = array(
			'confirm_admin'=> $_SESSION['admin']['name'],
			'confirm_time'=> time(),
			'class_status'=>CLASS_STATUS_1
		);
		HttpDaoManager::getBookCourseDao()->update($data, $id);
		
		$tmp = array(
			'type' =>BOOK_COURSE_LOG_TYPE_1,
			'remark' =>$remark,
			'book_course_id' => $id
		);
		
		HttpServerManager::getBookCourseLogServer()->add($tmp);
	}
	public function updateTeacherAndStudenStatus($id,$info_new,$remark,$log_type){
		$data = array(
			'report_status'=>$info_new['report_status'],
			'teacher_status'=>$info_new['teacher_status'],
			'user_status'=>$info_new['user_status'],
			'teacher_id'=>$info_new['teacher_id'],
			'substitute'=>$info_new['substitute'],
			'original_teacher_id'=>$info_new['original_teacher_id'],
			'teacher_class_id'=>$info_new['teacher_class_id']
		);
		HttpDaoManager::getBookCourseDao()->update($data, $id);

		$tmp = array(
			'type' =>$log_type,
			'remark' =>$remark,
			'book_course_id' => $id
		);
		
		HttpServerManager::getBookCourseLogServer()->add($tmp);
	}
	
	public function completeComplaint($id,$remark =''){
		$tmp = array(
				'type' =>BOOK_COURSE_LOG_TYPE_6,
				'remark' =>$remark,
				'book_course_id' => $id
		);
		//工资这一块全部重做，不采取日志方式，采取单条记录汇总方式
		//另外，需防止用户重复提交记录
		//每次重新，计算，删除老师关于该课的记录，根据
		//该课程的老师状态，代课状态、报到状态，给老师基本的工资
		//这样避免不断的反冲
		//投诉需另外处理
		//1、正常上课情况
		//2、他人代课情况
		//3、请假代课情况
		//4、投诉情况，投诉，则扣工资或者减分，同一个预约单 
		//若是她的责任，则只能扣她一次
		HttpServerManager::getBookCourseLogServer()->add($tmp);		
		//对老师获得的工资进行反冲
		HttpServerManager::getTeacherLogServer()->reCacluteClassWage($id, $_SESSION['admin']['name']);
	}
	
	public function complete($id,$remark =''){
		$data = array(
				'complete_admin'=> $_SESSION['admin']['name'],
				'complete_time'=> time(),
				'class_status'=>CLASS_STATUS_2
		);
		$bookCourseDao  = HttpDaoManager::getBookCourseDao();
		$bookCourseDao->update($data, $id);

		$tmp = array(
				'type' =>BOOK_COURSE_LOG_TYPE_2,
				'remark' =>$remark,
				'book_course_id' => $id
		);
		
		HttpServerManager::getBookCourseLogServer()->add($tmp);
		$bookCourseData = $bookCourseDao->get($id);
		$integral = array(
				'integral' => 2,
				'user_id' => $bookCourseData['user_id'],
				'type' => INTEGRAL_TYPE_2
		);

		HttpServerManager::getUserServer()->updateIntegral($integral);
		HttpServerManager::getTeacherLogServer()->haveClassWage($id, $_SESSION['admin']['name']);//获得基本工资部分
	}
	public function  adminCancellation($id ,$remark='',$minutesAdvance=30){
		$return =   $this->cancellation($id,$_SESSION['admin']['name'] ,$minutesAdvance);
		if ($return==true){
			$tmp = array(
					'type' =>BOOK_COURSE_LOG_TYPE_3,
					'remark' =>$remark,
					'book_course_id' => $id
			);
			HttpServerManager::getBookCourseLogServer()->add($tmp);
		}
		return $return;
	}
	public function cancelTime($bookDate,$bookTime,$minutesAdvance=30){
		$booktime = $bookDate+(mb_substr($bookTime,0,2)*3600)+(mb_substr($bookTime,2,2)*60);
		$tmp = time()+$minutesAdvance*60;
		if ($tmp>$booktime){
			return  MESSAGE_TYPE_5;
		}
		return true;
	}
	
	public function cancellation($id,$adminNmae='',$minutesAdvance=30){
		$bookData = HttpDaoManager::getBookCourseDao()->get($id);
		if ($bookData['class_status']==3){
			return  MESSAGE_TYPE_5;
		}
		
		$tmpa = true;
		if($minutesAdvance>0){
			$tmpa   =  $this->cancelTime($bookData['book_date'], $bookData['book_time'], $minutesAdvance);
		}
	
		if ($tmpa===true){
			$data = array(
				'class_status' =>CLASS_STATUS_3,
				'cancel_admin' =>$adminNmae,
				'cancel_time' => time()
			);
			HttpDaoManager::getBookCourseDao()->update($data, $id);//取消订单

			$teachre_class_id = $bookData['teacher_class_id'];
			$teacher_class_info = array();
			$teacher_class_info['id'] = $teachre_class_id;
			$teacher_class_info['status'] = TEACHER_CLASS_STATUS_NOTBOOKING;
			HttpDaoManager::getTeacherStartCourseDao()->update($teacher_class_info,$teachre_class_id);
						
			$book_type1 = Common::_t('book_type1');
			$book_type1[$bookData['book_type']];
			
			$userdata = HttpDaoManager::getUserDao()->get($bookData['user_id']);
			$hour =  $userdata[$book_type1[$bookData['book_type']]] + $bookData['pay_userclass'];//归还用户所扣卡钳
			$userdatas = array(
				$book_type1[$bookData['book_type']] => $hour
			);
			HttpDaoManager::getUserDao()->update($userdatas, $bookData['user_id']);//更新
			$return = true;
			$tmp = array(
				'type'       => USER_CLASS_HOUR_LOG_2,
				'class_type' => $bookData['book_type'],
				'target_id'  => $id,
				'add_user'  => $adminNmae,
				'class_hour' => $bookData['pay_userclass'],
				'user_id'    => $bookData['user_id'],
				'remark'     => '取消课程'
			);
			HttpServerManager::getUserClassHourLogServer()->add($tmp);//日志记录
		}
		else{
			$return = $tmpa;
		}
		return  $return;
	}
	public function TeacherGet($teacherClassId){
		$data = array(
			'teacher_class_id' => $teacherClassId,
			'class_status'=> array(TEACHER_CLASS_STATUS_NOTBOOKING,'<>')
		);
		
		$tmp  =HttpDaoManager::getBookCourseDao()->searchList($data);
		if (!empty($tmp)){
			return  $tmp[0];
		}
	}
	public function lists($where){
		$order_by = 'order by id desc ';
		if($_SESSION['admin']['admin_type']==ADMIN_TYPE_TEACHER){
			$order_by = 'order by book_date desc,book_time desc ';	
		}
		
		$data =  HttpServerManager::getBookCourseServer()->pagingBySearch($where,$order_by);
		$book_types = Common::_t('book_types');
		$class_status = Common::_t('class_status');
		$course_type = Common::_t('course_type');
		$user_kind_pairs = Common::_t('user_kind');
		$user_id_list = Common::array_value_list($data['list'],'user_id');
		
		$user_pairs = array();
		if(!empty($user_id_list)){
			$user_list = HttpDaoManager::getUserDao()->getDataPairList($user_id_list);
			$employee_id_list = Common::array_value_list($user_list,'employee_id');
			$employee_list =  HttpDaoManager::getEmployeesDao()->getDataPairList($employee_id_list);
			$employee_pair = array();
			
			foreach($employee_list as $k=>$v){
				$employee_pair[$v['id']] = $v['name'];
			}
			
			foreach($user_list as $k=>$v){
				$user_pairs[$v['id']]['name']=$v['english_name'];
				$user_pairs[$v['id']]['qq']=$v['qq'];
				$user_pairs[$v['id']]['skype']=$v['skype_id'];
				$user_pairs[$v['id']]['phone']=$v['phone'];
				$user_pairs[$v['id']]['level']=$v['level'];
				$user_pairs[$v['id']]['job']=$v['job'];
				$user_pairs[$v['id']]['employee_id']=$v['employee_id'];
				$user_pairs[$v['id']]['employee_name']=$v['employee_id']!=0 ? $employee_pair[$v['employee_id']]:"无负责人";
				if($v['user_kind']==USER_KIND_ACTIVE_419||$v['user_kind']==USER_KIND_ACTIVE_515||$v['user_kind']==USER_KIND_ACTIVE_708){
					$user_pairs[$v['id']]['user_kind_name']=$user_kind_pairs[$v['user_kind']];
				}
			}	
			unset($user_list);	
		}
		$teacher_id_list = Common::array_value_list($data['list'],'teacher_id');
		$teacher_pairs = array();
		if(!empty($teacher_id_list)){
			$teacher_list = HttpDaoManager::getTeacherDao()->getDataPairList($teacher_id_list);
			foreach($teacher_list as $k=>$v){
				$teacher_pairs[$v['id']]=$v['serial_number'].'-'.$v['name'];
			}
			unset($teacher_list);
		}
		
		$course_id_list = Common::array_value_list($data['list'],'course_id');
		$course_pairs = array();
		if(!empty($course_id_list)){
			$course_list = HttpDaoManager::getCourseDao()->getDataPairList($course_id_list);
			foreach($course_list as $k=>$v){
				$course_pairs[$v['id']]=$v['serial_number'].'-'.$v['theme'];
			}
			unset($course_list);
		}
		$list = array();	
		foreach ($data['list'] as $k=>$v){
			$list[$k] = $v;
			$list[$k]['book_type'] = $book_types[$v['book_type']];
			$list[$k]['class_status_name'] = $class_status[$v['class_status']];
			$list[$k]['course_type'] = $course_type[$v['course_type']];
			$list[$k]['user_name'] = $user_pairs[$v['user_id']]['name'];
			$list[$k]['user_qq'] = $v['qq'];
// 			$list[$k]['user_qq'] = $user_pairs[$v['user_id']]['qq'];
			$list[$k]['user_job'] = $user_pairs[$v['user_id']]['job'];
			$list[$k]['user_level'] = $user_pairs[$v['user_id']]['level'];
			$list[$k]['user_skype'] = $v['skype_id'];
// 			$list[$k]['user_skype'] = $user_pairs[$v['user_id']]['skype'];
			$list[$k]['user_phone'] = $v['phone'];
			$list[$k]['english_name'] = $v['english_name'];
// 			$list[$k]['user_phone'] = $user_pairs[$v['user_id']]['phone'];
			$list[$k]['employee_name'] = $user_pairs[$v['user_id']]['employee_name'];
			$list[$k]['user_kind_name'] = $user_pairs[$v['user_id']]['user_kind_name'];
			$list[$k]['teacher_name'] = $teacher_pairs[$v['teacher_id']];
			$list[$k]['course_name'] = $course_pairs[$v['course_id']];
		}

		$data['list'] = $list;
		
		$data['bookTypesOptions'] = page_modifier_options($book_types,common::getGetStrValue('book_type'));
		$data['classStatusOptions'] = page_modifier_options($class_status,common::getGetStrValue('class_status'));
		return $data;
	}
	public function haveClass($courseRecord,$id){
		$bookData = HttpDaoManager::getBookCourseDao()->get($id);
			
		if ($bookData['class_status'] !=CLASS_STATUS_2){
			if($courseRecord['user_status']==BOOK_USER_STATUS_2){
				$data['teacher_status'] = BOOK_TEACHER_STATUS_2;//未上课
				$data['user_status'] = $courseRecord['user_status'];//未报到
			}else{
				$data['teacher_status'] = BOOK_TEACHER_STATUS_1;//已上课
				$data['user_status'] = $courseRecord['user_status'];
			}
			
			HttpDaoManager::getBookCourseDao()->update($data, $id);//学生状态更新
			if($bookData['class_status']==CLASS_STATUS_0){//没确认,确认
				$this->confirm($id);//更新确认状态
			}
			
			if($bookData['original_teacher_id']==0){//没有代课情况,可以更新，代课情况，客服处理时，已经更新
				$data = array('id'=>$bookData['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_CLASSED);//已上课
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);//预约课信息状态由预约更改为上课
			}
			
			$msg = Common::_t('book_course_class_log');
			HttpServerManager::getBookCourseLogServer()->add(array('book_course_id'=>$id,'type'=>BOOK_COURSE_LOG_TYPE_1,'remark'=>$msg));
		}
	}
	
	public function haveCompleteClass($courseRecord,$id){
		$bookData = HttpDaoManager::getBookCourseDao()->get($id);
		if ($bookData['class_status'] !=CLASS_STATUS_2){
			$data['teacher_status'] = BOOK_TEACHER_STATUS_2;//已上课
			if($courseRecord['user_status']==BOOK_USER_STATUS_2){
				$data['teacher_status'] = BOOK_TEACHER_STATUS_1;//未上课
				$data['user_status'] = $courseRecord['user_status'];//未报到
			}else{
				$data['teacher_status'] = BOOK_TEACHER_STATUS_2;//已上课
				$data['user_status'] = $courseRecord['user_status'];
			}
			
			HttpDaoManager::getBookCourseDao()->update($data, $id);//学生报到
			
			if($bookData['original_teacher_id']==0){//没有代课情况,可以更新，代课情况，客服处理时，已经更新
				$data = array('id'=>$bookData['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_CLASSED);//无论，学生来了没有，都上课了
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
			}

			$msg = Common::_t('book_course_class_log');
			HttpServerManager::getBookCourseLogServer()->add(array('book_course_id'=>$id,'type'=>BOOK_COURSE_LOG_TYPE_2,'remark'=>$msg));
		}
	}
	
	public function userBook($where,$limit=''){
		$data = HttpDaoManager::getBookCourseDao()->searchList($where,'order by id desc',$limit);
		$list = array();
		$time = Common::getTimes();
		$bookType = Common::_t('book_types');
		$bookTeacherStatus = Common::_t('book_teacher_status');
		$bookUserStatus = Common::_t('book_user_status');
		$bookCourseStatus = Common::_t('class_status');
		$courseType = Common::_t('course_type');
		$communication_tools = Common::_t('user_communication');
		
		$courseDao = HttpDaoManager::getCourseDao();
		$teacherDao  = HttpDaoManager::getTeacherDao();
		$bookCourseServer = HttpServerManager::getBookCourseServer();
		
		foreach ($data as $k=>$v){
			$list[$k] = $v;
			$tmp = $time[$v['book_time']];
			$list[$k]['book_time_name'] = $tmp['name'].'-'.$tmp['nextName'];
			$list[$k]['book_type'] = $bookType[$v['book_type']];
			$courseData  = $courseDao->get($v['course_id']);
			$list[$k]['course'] = $courseData['theme'];
			$list[$k]['course_id'] = $courseData['id'];
			$list[$k]['download_url'] = $this->downloadUrl($v['course_id'], $v['chapter_id']);
			$teacherData = $teacherDao->get($v['teacher_id']);
			$list[$k]['teacher'] = $teacherData['name'];
			$list[$k]['teacher_portrait'] = $teacherData['detail_portrait'];
			$list[$k]['teacher_qq'] = $teacherData['qq'];
			$list[$k]['teacher_skype'] = $teacherData['skype'];
			$list[$k]['teacher_id'] = $teacherData['id'];
			$list[$k]['teacher_status_name'] = $bookTeacherStatus[$v['teacher_status']];
			$list[$k]['user_status_name'] = $bookUserStatus[$v['user_status']];
			$list[$k]['class_status_name'] = $bookCourseStatus[$v['class_status']];
			if(isset($v['course_type'])&&$v['course_type']>0){
				$list[$k]['course_type_name'] = $courseType[$v['course_type']];
			}
			$list[$k]['communication_tools_name'] = $communication_tools[$v['communication_tools']];
			$list[$k]['class_status_name'] = $bookCourseStatus[$v['class_status']];
			$list[$k]['cancellation'] = $bookCourseServer->cancelTime($v['book_date'], $v['book_time']);
		}
		
		return  $list;
	}
	
	public function bookCourseDetailInfo($id){
		$book_types = Common::_t('book_types');
		$class_status = Common::_t('class_status');

		$book = HttpDaoManager::getBookCourseDao()->get($id);

		$where = array();
		$where['book_time'] = $book['book_time'];
		$where['book_date'] = $book['book_date'];
		$nobook_teacher_list =  HttpDaoManager::getTeacherDao()->getNoBookTeacherByDateTime($where);		
		
		$book['book_type_name'] = $book_types[$book['book_type']];
		$book['class_status_name'] = $class_status[$book['class_status']];
		$tmp = Common::getTimes();
		$book['book_time'] = date('Y-m-d',$book['book_date']).' '.$tmp[$book['book_time']]['name'].'-'.$tmp[$book['book_time']]['nextName'];
		$userCommunication = Common::_t('user_communication'); 
		$book['communication_tools_name'] = $userCommunication[$book['communication_tools']];
		$teacherStatus = Common::_t('book_teacher_status');
		$book['teacher_status_list'] = $teacherStatus;
		$book['teacher_status_name'] = $teacherStatus[$book['teacher_status']];
		$userStatus = Common::_t('book_user_status');
		$book['user_status_list'] = $userStatus;
		$book['user_status_name'] = $userStatus[$book['user_status']];
		$payStatus = Common::_t('book_pay_status');
		$book['pay_status'] = $payStatus[$book['pay_status']];
		$course = HttpDaoManager::getCourseDao()->get($book['course_id']);
		$courseType = Common::_t('course_type');
		$course['type_name'] = $courseType[$course['type']];
		
		$courseLevel =  Common::_t('course_level');
		$course['level'] = $courseLevel[$course['level']];
		
		$teacher_report_status = Common::_t('teacher_report_status');	
		$book['teacher_report_status_list'] = $teacher_report_status;
		$book['teacher_report_status'] = $book['report_status'];
		
		$book_substitute = Common::_t('teacher_substitute');	
		$book['teacher_substitute_list'] = $book_substitute;
		$book['teacher_substitute'] = $book['substitute'];

		$book_substitute = Common::_t('teacher_substitute');
		$book['teacher_substitute_list'] = $book_substitute;
		$book['teacher_substitute'] = $book['substitute'];
		
		$user = HttpDaoManager::getUserDao()->get($book['user_id']);
		$sex = Common::_t('sex');
		if(isset($user['sex'])&&$user['sex']>0){
			$user['sex'] = $sex[$user['sex']];
		}
		$user['user_job_pair'] = Common::_t('user_job');
		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$user['employee_name'] = "";
		
		if(isset($user['employee_id'])&&$user['employee_id']>0){
			$employee_info =  HttpDaoManager::getEmployeesDao()->getDataPairList(array($user['employee_id']));		
			if(!empty($employee_info)){
				$user['employee_name'] = $employee_info[0]['name'];
			}
		}
		
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}
		unset($level_list);
		$user['level_pair'] = $level_pair;		
		
		
		$teacher = HttpDaoManager::getTeacherDao()->get($book['teacher_id']);
		
		$substitue_teacher_pair = array();		
		if($book['substitute']==BOOK_SUBSTITUTE_1){
			$book['old_teacher_id'] = $book['teacher_id'];
			$book['old_teacher_name'] = $teacher['name'].'-'.$teacher['serial_number'];
			$substitue_teacher_pair[$book['old_teacher_id']] = 'original Teacher:'.$book['old_teacher_name'];
		}else{
			$book['old_teacher_id'] = $book['original_teacher_id'];
			$old_teacher = HttpDaoManager::getTeacherDao()->get($book['old_teacher_id']);
			$book['old_teacher_name'] = $old_teacher['name'].'-'.$old_teacher['serial_number'];
			$substitue_teacher_pair[$book['old_teacher_id']] = 'original Teacher:'.$book['old_teacher_name'];
			$substitue_teacher_pair[$book['teacher_id']] = 'substitute Teacher:'.$teacher['name'].'-'.$teacher['serial_number'];
		}
		
		foreach($nobook_teacher_list as $key=>$value){
			$substitue_teacher_pair[$value['id']]='substitute Teacher:'.$value['name'].'-'.$value['serial_number'];
		}
		
		$book['substitue_teacher_pair'] = $substitue_teacher_pair;
		$file_name = getLangStr($book['chapter_name'],'en_us');
		if($file_name!=""){
			$english_name = getLangStr($book['chapter_name'],'en_us');
			$english_name = str_replace('?','',$english_name);
			$book['download_filename'] = '/'.preg_replace('/\s{2, }| /','_',$english_name).'.pdf';
		}
		
		$nationality  = Common::_t('nationality');
		$teacher['nationality'] = $nationality[$teacher['nationality']];
		$contractCategory = Common::_t('contract_category');
		$teacher['employee_name'] = "";
		
		if(isset($teacher['employees_id'])&&$teacher['employees_id']>0){
			$employee_info =  HttpDaoManager::getEmployeesDao()->getDataPairList(array($teacher['employees_id']));
			if(!empty($employee_info)){
				$teacher['employee_name'] = $employee_info[0]['name'];
			}
		}		
		
		$teacher['contract_category'] = $contractCategory[$teacher['contract_category']];
		$teacher['good_course'] = str_replace('|','',$teacher['good_course']);
		$teacher['good_course'] = explode(',',$teacher['good_course']);
		
		$course_types = Common::_t('course_type');
		$tmp = array();
		foreach ($teacher['good_course'] as $v){
			$tmp[]=  $course_types[$v];
		}
		
		$teacher['good_course'] = implode(',',$tmp);
		$teacher['sex'] = $sex[$teacher['sex']];
		$employees = HttpDaoManager::getEmployeesDao()->get($teacher['employees_id']);
		$teacher['employees'] = $employees['name'];
		$log = HttpDaoManager::getBookCourseLogDao()->getListByBookCourseId($book['id']);
		$type = Common::_t('book_log_type');
		foreach ($log as $k=>$v){
			$log[$k]['type'] = $type[$log[$k]['type']];
		}
		$ret_data =array('book'=>$book,'course'=>$course,'user'=>$user,'teacher'=>$teacher,'log'=>$log); 
		return $ret_data;
	}
	
	public function getUserNofityBookList(){//05,35分执行,插入列表
		$where = array();
		$where['book_date'] = strtotime(date('Y-m-d'));
		$where['class_status'] = CLASS_STATUS_0;//未上课
		$where['book_time'] = Common::getDefaultBeforeOpenTime();//提前1个小时短信通知
		$sms_notiry_list_str = implode(',',array(BOOKCOURSE_SMS_NOTIFY_DEFAULT,BOOKCOURSE_SMS_NOTIFY_FAIL));
		$where['sms_notify']= array($sms_notiry_list_str,'in');

		$BookCoursedata = HttpDaoManager::getBookCourseDao()->searchList($where,'order by id desc',100);

		if(!empty($BookCoursedata)){
			$user_id_list = Common::array_value_list($BookCoursedata, 'user_id');
			$where = array();
			$user_id_list_str = implode(',',$user_id_list);
			$where['user_id_list_str'] = $user_id_list_str;
			$userData = HttpDaoManager::getUserDao()->searchList($where);
			$user_sms_server = array();
			foreach($userData as $key=>$value){
				$user_sms_server[$value['id']]=$value['sms_server'];
			}
			
			$userBookListData = array();
			$sms_server_noneed = array();
			$book_times = Common::getTimes();
			$communication_tools_pair = Common::_t('user_communication');
			
			$sms_send_data = array();
			foreach($BookCoursedata as $key=>$value){
				if(isset($user_sms_server[$value['user_id']])&&$user_sms_server[$value['user_id']]==SMS_SERVER_2){
					array_push($sms_server_noneed, $value['id']);
					continue;
				}
				$book_date_time = date('Y-m-d',$value['book_date'])." ".$book_times[$value['book_time']]['name'];
				$book_tool = $communication_tools_pair[$value['communication_tools']];
				$book_id = $value['id'];
				$content="您好，您所预定的英语课堂将在".$book_date_time."开课,通讯工具为".$book_tool.",请提前准备,祝您听课愉快!";
				
				$sms_send_data[$value['id']]['content'] =  $content;
				$sms_send_data[$value['id']]['target_id'] =  $value['id'];
				$sms_send_data[$value['id']]['phone'] =  $value['phone'];
			}
			
			if(!empty($sms_server_noneed)){
				$id_list_str = implode(',',$sms_server_noneed);
				$updata = array();
				$updata['sms_notify'] = BOOKCOURSE_SMS_NOTIFY_NONEED;
				HttpDaoManager::getBookCourseDao()->updateByIdList($data, $id_list_str);
			}
			
			$send_time = time();
			$send_tmp = array();
			$tmp1 = array();
			foreach ($sms_send_data as $v){
				$tmp1['phone'] = $v['phone'];
				$tmp1['content'] = $v['content'];
				$tmp1['time'] = $send_time;
				$tmp1['target_id'] = $v['target_id'];
				array_push($send_tmp,$tmp1);
			}
			HttpDaoManager::getMobileSendlistDao()->addArray($send_tmp);//插入发送列表			
		}
		
		return false;
	}
	
	public function downloadUrl($course_id,$chapter_id){
		$tmp =HttpServerManager::getCourseServer()->downloadMaterial($chapter_id,$course_id);
		$file_name="";
		if($tmp!=""){
			$pos= strrpos($tmp,'/');
			$file_head = substr($tmp, 1,$pos);
			$file_name = substr($tmp, $pos+1);
			$file_name = '/'.preg_replace('/\s{2, }| /','_',$file_name);
		}
		$course_sa = Common::encryptUrl($course_id);
		$course_up = Common::encryptUrl($chapter_id);
		$download_url = SITE_URL.'user_downloadMaterial.html'.$file_name.'?sa='.$course_sa.'&up='.$course_up;
		return $download_url;
	}
}
?>